<?php

namespace handle\message\log;

use auxiliary\traits\Singleton;

class CustomException
{
    use Singleton;

    protected function beforeOnlyUseOnce(){
        set_exception_handler([$this, 'handler']);
    }

    public function handler($exceptions) {
        if($exceptions->getMessage() !== null) {
            $error = 'error msg: ' . $exceptions->getMessage() . PHP_EOL . 'file: ' . $exceptions->getFile() . PHP_EOL . 'line: ' . $exceptions->getLine() . PHP_EOL . 'code: ' . $exceptions->getCode() . PHP_EOL . 'function: ' . $exceptions->getTrace()[0]['function'] . '()' . PHP_EOL;
            if(!empty($exceptions->getTrace()) and is_array($exceptions->getTrace())){
                foreach($exceptions->getTrace() as $key=>$value){
                    if(!isset($value['file'])){
                        $value['file'] = '';
                    }
                    if(!isset($value['line'])){
                        $value['line'] = '';
                    }
                    $error .= "previous file: ".$value['file']. PHP_EOL . 'line: ' .$value['line'] . PHP_EOL . "function: ".$value['function']."()." . PHP_EOL;
                }
            }
            putErrorLog($error);
        }
        putLog('/aaa.log' , json_encode($exceptions));
        putLog('/bbb.log' , serialize($exceptions));
    }

}